package Collections;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Collections工具类中的sort()方法如何比较元素？
 * 排序
 */
public class test {
    @Test
    public void test(){
        List<Students> ss = new ArrayList<Students>();
        ss.add(new Students("df",-1));
        ss.add(new Students("df1",-1));
        ss.add(new Students("df2",6));
        ss.add(new Students("df3",2));
        Collections.sort(ss, new Comparator<Students>() {
            @Override
            public int compare(Students o1,Students o2) {
                return o2.getAge() - o1.getAge();
            }
//            if (dt1.getTime() < dt2.getTime()) {
//                return 1;
//            } else if (dt1.getTime() > dt2.getTime()) {
//                return -1;
//            } else {
//                return 0;
//            }
        });
        for(int i =0;i<ss.size();i++){
            System.out.println(ss.get(i).toString());
        }
//        Students [name=df2, age=6]
//        Students [name=df1, age=4]
//        Students [name=df3, age=2]
//        Students [name=df, age=1]
        //
        Collections.reverse(ss);
        System.out.println("-------------------");

        for(int i =0;i<ss.size();i++){
            System.out.println(ss.get(i).toString());
        }
    }
}
